
package com.youbo.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.youbo.common.utils.MatrixToImageWriter;
import com.youbo.common.utils.R;
import com.youbo.entity.JnOrderEntity;
import com.youbo.form.RequestOrder;
import com.youbo.service.JnOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;

/**
 * 济宁银行支付接口
 *
 * @author macy shaoyecf@163.com
 */
@RestController
@RequestMapping("/pay")
@Api(tags="济宁银行支付接口")
public class ApiPayController {

    @Autowired
    JnOrderService jnOrderService;

    @RequestMapping(value = "createOrder",method = {RequestMethod.POST,RequestMethod.GET})
    @ApiOperation("创建订单")
    public R createOrder(@ApiParam("订单号") @RequestParam String orderNo, @ApiParam("订单标题") @RequestParam String title, @ApiParam("订单描述") @RequestParam String description, @ApiParam("订单金额") @RequestParam Double amount, @ApiParam("授权码") @RequestParam(required=false) String authcode){
        if(description.equals("ghf")){
            title="挂号费";
            description="挂号费";
        }if(description.equals("yyf")){
            description="医药费";
            title="医药费";
        }if(description.equals("zyf")){
            description="住院费";
            title="住院费";
        }

        RequestOrder order=new RequestOrder(orderNo,title,description,amount,authcode);

        return jnOrderService.createOrder(order);
    }

    @RequestMapping(value = "queryOrder",method = {RequestMethod.POST,RequestMethod.GET})
    @ApiOperation("查询订单")
    public R queryOrder(@ApiParam("订单号") @RequestParam String orderNo){
        RequestOrder order=new RequestOrder(orderNo);
        return jnOrderService.queryOrder(order);
    }

    @RequestMapping(value = "outOrder",method = {RequestMethod.POST,RequestMethod.GET})
    @ApiOperation("退款")
    public R queryOrder(@ApiParam("订单号") @RequestParam String orderNo, @ApiParam("退款金额") @RequestParam Double amount){
        RequestOrder order=new RequestOrder(orderNo,null,null,amount);
        return jnOrderService.outOrder(order);
    }




    @RequestMapping(value = "qcCode",method = {RequestMethod.POST,RequestMethod.GET})
    @ApiOperation("二维码连接")
    @ResponseBody
    public void qcCode(@ApiParam("订单号") @RequestParam String orderNo, @ApiIgnore HttpServletResponse response)  throws IOException {
        JnOrderEntity order=jnOrderService.getOne(new QueryWrapper<JnOrderEntity>().eq("sub_no",orderNo));
        String qc_url="";
        if(null==order){
            qc_url="订单不存在";
        }else{
            qc_url=order.getCodeUrl();
        }

        //将图片输出给浏览器
        response.setContentType("image/JPEG");
        OutputStream os = response.getOutputStream();
        ImageIO.write(MatrixToImageWriter.writeInfoToJpgBuff(qc_url), "JPEG", os);

    }



    @RequestMapping(value = "payCallback",method = {RequestMethod.POST,RequestMethod.GET})
    @ApiOperation("支付回调")
    @ResponseBody
    public String payCallback(@ApiIgnore HttpServletRequest request, @ApiIgnore HttpServletResponse response){
//        Map map = Tools.getParameterMap(request);
//        //String epay_rrn=Tools.toString("epay_rrn");
//        String payer_info=Tools.toString(map.get("payer_info"));
//        String payer_channel=Tools.toString(map.get("payer_channel"));
//        String payer_datetime=Tools.toString(map.get("payer_datetime"));
//        //String payer_payamt=Tools.toString("payer_payamt");
//        String trans_state=Tools.toString(map.get("trans_state"));//0-成功 1-失败 8-已撤销 9-处理中
//
//        System.out.println(map.toString());

        return "";
    }


}
